﻿using BotDetector.Core.Model;

namespace BotDetector.Core.Search
{
    public class GoogleBingAggregator
    {
        public ResultItemList AggregateGoogleAndBingResultsItemLists(ResultItemList googleResults, ResultItemList bingResults)
        {
            ResultItemList finalResults = new ResultItemList
            {
                Phrase = googleResults.Phrase
            };
            finalResults.AddRange(googleResults);
            CheckIfBingUrlsExistsInGoogleUrls(googleResults, bingResults, finalResults);
            finalResults.AddRange(googleResults);
            return finalResults;
        }

        private static void CheckIfBingUrlsExistsInGoogleUrls(ResultItemList googleResults, ResultItemList bingResults, ResultItemList finalResults)
        {
            int k = 0;
            foreach (ResultItem bingResultItem in bingResults)
            {
                k = CheckIfAppearsInOtherResults(googleResults, bingResultItem);
                if (k == 0)
                {
                    finalResults.Add(bingResultItem);
                }
            }
        }

        private static int CheckIfAppearsInOtherResults(ResultItemList googleResults, ResultItem bingResultItem)
        {
            int k = 0;
            foreach (ResultItem googleResultItem in googleResults)
            {
                if (bingResultItem.Url == googleResultItem.Url)
                {
                    k = 1;
                }
            }
            return k;
        }
    }
}
